// pages/addAddressInfo/addAddressInfo.js
const app = getApp()
import api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true,
    province: '',
    city: '',
    county: ''
  },
  // 选择省
  goToSelectProvince() {
    let obj = {
      p_id: 0
    }
    api.POST('/applet/area/findAll', obj, res => {
      console.log(res)
      app.globalData.provinceArr = res.data.data
      wx.navigateTo({
        url: '/pages/province/province'
      })
    })

  },

  // 点击按钮保存地址信息
  saveAddressInfo(e) {
    console.log(e.detail.value)
    let params = e.detail.value
    if (params.phone.length === 0 || params.name.length === 0 || params.address.length === 0) {
      wx.showToast({
        title: '姓名,手机号码和地址不能为空！',
        icon: 'none',
        duration: 1500
      })
      return;
    }

    let myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(params.phone)) {
      wx.showToast({
        title: '请填写正确的手机号码！',
        icon: 'none',
        duration: 1500
      })
      return;
    }


    if (params.status) {
      params.status = 2
    } else {
      params.status = 1
    }

    params.province_name = this.data.province
    params.province_code = app.globalData.province_code
    params.city_name = this.data.city
    params.city_code = app.globalData.city_code
    params.area_name = this.data.county
    params.area_code = app.globalData.county_code
    params.status = params.status

    app.globalData.addressInfo = params

    api.POST('/applet/address/create', params, res => {
      console.log(res)

      wx.navigateBack({
        delta: 1
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


    if (app.globalData.selectedProvince) {
      console.log(app.globalData)
      this.setData({
        province: app.globalData.selectedProvince,
        city: app.globalData.selectedCity,
        county: app.globalData.selectedCounty,
        isShow: false
      })
    }



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})